-- gather_suit
-- Created by chengb Sep/11/2015
-- 成就：收集套装

-- 内部函数声明
local whenSuitActive;

-- 注册套装激活的回调
EventMgr.register("achievements/gather_suit", event.SUIT_ACTIVE, function(args)
    whenSuitActive(args);
end);

-- 注册套装激活的回调
whenSuitActive = function(args)
    -- 获取已经激活的套装记录
    local activeSuits = ME.user.dbase:query("active_suits", {});
    
    -- 获取当前所在的迷宫编号
    local dungeonId = DungeonM.getDungeonId();
    
    -- 判断是否是符合要求的成就
    local function isValidId(id)
        if true == AchievementM.isCompleted(id) then
            -- 已经完成了
            return false;
        end

        if not AchievementM.isBelongTo(id, dungeonId) then
            -- 不是该迷宫的成就
            return false;
        end

        -- 是否收齐指定的套装
        local isComplete = true;

        local config = AchievementM.query(id);
        local ruleArg = config["rule_arg"];
        if type(ruleArg) == 'table' and type(ruleArg["suits"]) == 'table' then
            for _, suitId in pairs(ruleArg["suits"]) do
                if table.indexOf(activeSuits, suitId) == -1 then
                    -- 还有套装没收齐
                    isComplete = false;
                end
            end
        end

        return isComplete;
    end

    -- 遍历所有的子规则
    local ids = AchievementM.getIdsByRule("gather_suit");
    for _, id in pairs(ids) do
        if isValidId(id) then
            -- 尝试完成成就
            AchievementM.tryComplete(id, 1);
        end
    end
end

return {

};